home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GXPATH2 < prev    next >
Text File  |  1991-10-26  |  13KB  |  416 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxpath2.c */
  21. /* Path tracing procedures for Ghostscript library */
  22. #include "math_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxarith.h"
  27. #include "gzpath.h"
  28.  
  29. /* Forward declarations */
  30. private int copy_path(P3(gx_path *, gx_path *, fixed));
  31. private int flatten_recur(P8(gx_path *,
  32.   fixed, fixed, fixed, fixed, fixed, fixed, fixed));
  33.  
  34. /* Read the current point of a path. */
  35. int
  36. gx_path_current_point(gx_path *ppath, gs_fixed_point *ppt)
  37. {    if ( !ppath->position_valid )
  38.       return_error(gs_error_nocurrentpoint);
  39.     /* Copying the coordinates individually */
  40.     /* is much faster on a PC, and almost as fast on other machines.... */
  41.     ppt->x = ppath->position.x, ppt->y = ppath->position.y;
  42.     return 0;
  43. }
  44.  
  45. /* Read the bounding box of a path. */
  46. int
  47. gx_path_bbox(gx_path *ppath, gs_fixed_rect *pbox)
  48. {    if ( ppath->first_subpath == 0 )
  49.        {    /* The path is empty, use the current point if any. */
  50.         gx_path_current_point(ppath, &pbox->p);
  51.         return gx_path_current_point(ppath, &pbox->q);
  52.        }
  53.     /* The stored bounding box may not be up to date. */
  54.     /* Correct it now if necessary. */
  55.     if ( ppath->box_last == ppath->current_subpath->last )
  56.        {    /* Box is up to date */
  57.         *pbox = ppath->bbox;
  58.        }
  59.     else
  60.        {    gs_fixed_rect box;
  61.         register segment *pseg = ppath->box_last;
  62.         if ( pseg == 0 )    /* box is uninitialized */
  63.            {    pseg = (segment *)ppath->first_subpath;
  64.             box.p.x = box.q.x = pseg->pt.x;
  65.             box.p.y = box.q.y = pseg->pt.y;
  66.            }
  67.         else
  68.            {    box = ppath->bbox;
  69.             pseg = pseg->next;
  70.            }
  71. /* Macro for adjusting the bounding box when adding a point */
  72. #define adjust_bbox(pt)\
  73.   if ( (pt).x < box.p.x ) box.p.x = (pt).x;\
  74.   else if ( (pt).x > box.q.x ) box.q.x = (pt).x;\
  75.   if ( (pt).y < box.p.y ) box.p.y = (pt).y;\
  76.   else if ( (pt).y > box.q.y ) box.q.y = (pt).y
  77.         while ( pseg )
  78.            {    switch ( pseg->type )
  79.                {
  80.             case s_curve:
  81. #define pcurve ((curve_segment *)pseg)
  82.                 adjust_bbox(pcurve->p1);
  83.                 adjust_bbox(pcurve->p2);
  84. #undef pcurve
  85.                 /* falls through */
  86.             default:
  87.                 adjust_bbox(pseg->pt);
  88.                }
  89.             pseg = pseg->next;
  90.            }
  91. #undef adjust_bbox
  92.         ppath->bbox = box;
  93.         ppath->box_last = ppath->current_subpath->last;
  94.         *pbox = box;
  95.        }
  96.     return 0;
  97. }
  98.  
  99. /* Test if a path has any curves. */
  100. int
  101. gx_path_has_curves(gx_path *ppath)
  102. {    return ppath->curve_count != 0;
  103. }
  104.  
  105. /* Test if a path has any segments. */
  106. int
  107. gx_path_is_void(gx_path *ppath)
  108. {    return ppath->segment_count == 0;
  109. }
  110.  
  111. /* Test if a path is a rectangle. */
  112. /* If so, return its bounding box. */
  113. int
  114. gx_path_is_rectangle(gx_path *ppath, gs_fixed_rect *pbox)
  115. {    subpath *pseg0;
  116.     if (    ppath->subpath_count == 1 &&
  117.         ppath->segment_count == 4 && ppath->curve_count == 0 &&
  118.         (pseg0 = ppath->first_subpath)->last->type == s_line_close )
  119.        {    fixed x0 = pseg0->pt.x, y0 = pseg0->pt.y;
  120.         segment *pseg1 = pseg0->next;
  121.         segment *pseg2 = pseg1->next;
  122.         fixed x2 = pseg2->pt.x, y2 = pseg2->pt.y;
  123.         segment *pseg3 = pseg2->next;
  124.         if (    (x0 == pseg1->pt.x && pseg1->pt.y == y2 &&
  125.              x2 == pseg3->pt.x && pseg3->pt.y == y0) ||
  126.             (x0 == pseg3->pt.x && pseg3->pt.y == y2 &&
  127.              x2 == pseg1->pt.x && pseg1->pt.y == y0)
  128.            )
  129.            {    /* Path is a rectangle.  Return bounding box. */
  130.             if ( x0 < x2 )
  131.                 pbox->p.x = x0, pbox->q.x = x2;
  132.             else
  133.                 pbox->p.x = x2, pbox->q.x = x0;
  134.             if ( y0 < y2 )
  135.                 pbox->p.y = y0, pbox->q.y = y2;
  136.             else
  137.                 pbox->p.y = y2, pbox->q.y = y0;
  138.             return 1;
  139.            }
  140.        }
  141.     return 0;
  142. }
  143.  
  144. /* Return the quick-check rectangle for a clipping path. */
  145. /* This only works for paths that have gone through set_clip_path. */
  146. /* which is why the name is different. */
  147. int
  148. gx_cpath_box_for_check(register gx_path *ppath, gs_fixed_rect *pbox)
  149. {    *pbox = ppath->cbox;
  150.     return 0;
  151. }
  152.  
  153. /* Test if a clipping path includes a rectangle. */
  154. /* The rectangle need not be oriented correctly, i.e. x0 > x1 is OK. */
  155. /* This only works for paths that have gone through set_clip_path. */
  156. /* which is why the name is different. */
  157. int
  158. gx_cpath_includes_rectangle(register gx_path *ppath,
  159.   fixed x0, fixed y0, fixed x1, fixed y1)
  160. {    return
  161.         (x0 <= x1 ?
  162.             (ppath->cbox.p.x <= x0 && x1 <= ppath->cbox.q.x) :
  163.             (ppath->cbox.p.x <= x1 && x0 <= ppath->cbox.q.x)) &&
  164.         (y0 <= y1 ?
  165.             (ppath->cbox.p.y <= y0 && y1 <= ppath->cbox.q.y) :
  166.             (ppath->cbox.p.y <= y1 && y0 <= ppath->cbox.q.y));
  167. }
  168.  
  169. /* Copy a path */
  170. int
  171. gx_path_copy(gx_path *ppath_old, gx_path *ppath)
  172. {    return copy_path(ppath_old, ppath, (fixed)0);
  173. }
  174.  
  175. /* Merge a path into its parent (the path in the previous graphics */
  176. /* context).  If ppto is not the parent of ppfrom, chaos may result! */
  177. int
  178. gx_path_merge(gx_path *ppfrom, gx_path *ppto)
  179. {    /* If no new segments, don't release the parent. */
  180.     subpath *psfrom = ppfrom->current_subpath;
  181.     subpath *psto = ppto->current_subpath;
  182.     if ( psto != 0 && psfrom->last != psto->last )
  183.        {    gx_path_release(ppto);
  184.        }
  185.     *ppto = *ppfrom;
  186.     ppfrom->shares_segments = 1;
  187.     return 0;
  188. }
  189.  
  190. /* Translate an already-constructed path (in device space). */
  191. /* Don't bother to translate the cbox. */
  192. int
  193. gx_path_translate(gx_path *ppath, fixed dx, fixed dy)
  194. {    segment *pseg;
  195. #define translate_xy(pt)\
  196.   pt.x += dx, pt.y += dy
  197.     translate_xy(ppath->bbox.p);
  198.     translate_xy(ppath->bbox.q);
  199.     translate_xy(ppath->position);
  200.     pseg = (segment *)(ppath->first_subpath);
  201.     while ( pseg )
  202.        {    switch ( pseg->type )
  203.            {
  204.         case s_curve:
  205.            {    curve_segment *pc = (curve_segment *)pseg;
  206.             translate_xy(pc->p1);
  207.             translate_xy(pc->p2);
  208.            }
  209.         default:
  210.             translate_xy(pseg->pt);
  211.            }
  212.         pseg = pseg->next;
  213.        }
  214.     return 0;
  215. }
  216.  
  217. /* Flatten a path */
  218. int
  219. gx_path_flatten(gx_path *ppath_old, gx_path *ppath, floatp flatness)
  220. {    /* See the flattening algorithm below for an explanation of */
  221.     /* the following computation. */
  222.     fixed scaled_flat = float2fixed(flatness);
  223.     if ( scaled_flat > int2fixed(100) )
  224.         scaled_flat = int2fixed(100);
  225.     else if ( scaled_flat <= float2fixed(0.2) )
  226.         scaled_flat = float2fixed(0.2);
  227.     return copy_path(ppath_old, ppath, scaled_flat);
  228. }
  229.  
  230. /* Copy a path, optionally flattening it. */
  231. /* If the copy fails, free the new path. */
  232. private int
  233. copy_path(gx_path *ppath_old, gx_path *ppath, fixed scaled_flat)
  234. {    gx_path old;
  235.     segment *pseg;
  236.     int code;
  237. #ifdef DEBUG
  238. if ( gs_debug['p'] )
  239.     gx_dump_path(ppath_old, "before copy_path");
  240. #endif
  241.     old = *ppath_old;
  242.     gx_path_init(ppath, &ppath_old->memory_procs);
  243.     pseg = (segment *)(old.first_subpath);
  244.     while ( pseg )
  245.        {    switch ( pseg->type )
  246.            {
  247.         case s_start:
  248.             code = gx_path_add_point(ppath, pseg->pt.x, pseg->pt.y);
  249.             break;
  250.         case s_curve:
  251.            {    curve_segment *pc = (curve_segment *)pseg;
  252.             if ( scaled_flat == 0 )    /* don't flatten */
  253.                 code = gx_path_add_curve(ppath,
  254.                     pc->p1.x, pc->p1.y,
  255.                     pc->p2.x, pc->p2.y,
  256.                     pc->pt.x, pc->pt.y);
  257.             else
  258.                 code = flatten_recur(ppath,
  259.                     pc->p1.x, pc->p1.y,
  260.                     pc->p2.x, pc->p2.y,
  261.                     pc->pt.x, pc->pt.y,
  262.                     scaled_flat);
  263.             break;
  264.            }
  265.         case s_line:
  266.             code = gx_path_add_line(ppath, pseg->pt.x, pseg->pt.y);
  267.             break;
  268.         case s_line_close:
  269.             code = gx_path_close_subpath(ppath);
  270.             break;
  271.            }
  272.         if ( code )
  273.            {    gx_path_release(ppath);
  274.             if ( ppath == ppath_old ) *ppath_old = old;
  275.             return code;
  276.            }
  277.         pseg = pseg->next;
  278.     }
  279.     ppath->position = old.position;        /* restore current point */
  280. #ifdef DEBUG
  281. if ( gs_debug['p'] )
  282.     gx_dump_path(ppath, "after copy_path");
  283. #endif
  284.     return 0;
  285. }
  286. /* Internal routine to flatten a curve. */
  287. /* This calls itself recursively, using binary subdivision, */
  288. /* until the approximation is good enough to satisfy the */
  289. /* flatness requirement.  The starting point is ppath->position, */
  290. /* which gets updated as line segments are added. */
  291.  
  292. /* Table of f(i) = 256 * sqrt(1 + (i/64)^2). */
  293. /* This is good to within 1% or better. */
  294. #define scaled_sqrt_shift 6        /* scaling of index */
  295. #define sqrt_scale_shift 8        /* scaling of value */
  296. private int scaled_sqrt_tab[65] =
  297.    {    256, 256, 256, 256, 256, 256, 257, 257,
  298.     257, 258, 259, 259, 260, 261, 262, 262,
  299.     263, 264, 265, 267, 268, 269, 270, 272,
  300.     273, 274, 276, 277, 279, 281, 282, 284,
  301.     286, 288, 289, 291, 293, 295, 297, 299,
  302.     301, 304, 306, 308, 310, 312, 315, 317,
  303.     320, 322, 324, 327, 329, 332, 334, 337,
  304.     340, 342, 345, 348, 350, 353, 356, 359,
  305.     362
  306.    };
  307.  
  308. private int
  309. flatten_recur(gx_path *ppath,
  310.   fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3,
  311.   fixed scaled_flat)
  312. {    fixed
  313.       x0 = ppath->position.x,
  314.       y0 = ppath->position.y;
  315. top:
  316. #ifdef DEBUG
  317. if ( gs_debug['2'] )
  318.     dprintf4("[2]x0=%f y0=%f x1=%f y1=%f\n",
  319.          fixed2float(x0), fixed2float(y0),
  320.          fixed2float(x1), fixed2float(y1)),
  321.     dprintf4("   x2=%f y2=%f x3=%f y3=%f\n",
  322.          fixed2float(x2), fixed2float(y2),
  323.          fixed2float(x3), fixed2float(y3));
  324. #endif
  325.     /*
  326.      * Compute the maximum distance of the curve from
  327.      * the line (x0,y0)->(x3,y3).  We do this conservatively
  328.      * by observing that the curve is enclosed by the
  329.      * quadrilateral of its control points, so we simply
  330.      * compute the distances of (x1,y1) and (x2,y2)
  331.      * from the line.  Letting dx = x3-x0 and dy = y3-y0,
  332.      * the distance of (xp,yp) from the line is
  333.      * abs(N)/sqrt(D), where N = dy*(xp-x0)-dx*(yp-y0) and
  334.      * D = dx*dx+dy*dy; hence we want to test abs(N) <= sqrt(D)*F,
  335.      * where F is the flatness parameter from the graphics state.
  336.      * We can do this more efficiently by letting t=dy/dx, and
  337.      * testing abs(N1) <= sqrt(D1)*f, where N1=t*(xp-x0)-(yp-y0) and
  338.      * D1 = 1+t*t.  If dx < dy, we swap x and y for this
  339.      * computation.  This guarantees abs(t) <= 1, which allows us to
  340.      * compute sqrt(1+t*t) by table lookup on the high bits of abs(t).
  341.      */
  342.      { fixed dx3 = x3 - x0;
  343.        fixed adx3 = any_abs(dx3);
  344.        fixed dy3 = y3 - y0;
  345.        fixed ady3 = any_abs(dy3);
  346.        /* We have to be quite careful to ensure that */
  347.        /* none of the multiplications will overflow. */
  348. #define short_max 0x7ff0L
  349. #define reduce_3(ad3, maxv)\
  350.   while ( ad3 > maxv )\
  351.     adx3 >>= 1, ady3 >>= 1,\
  352.     dx3 = arith_rshift(dx3, 1), dy3 = arith_rshift(dy3, 1)
  353. #define reduce_d(d)\
  354.   for ( shift = 0; (d < 0 ? d < -short_max : d > short_max); shift++ )\
  355.     d = arith_rshift(d, 1)
  356.        if ( adx3 > ady3 )
  357.         {    fixed d, dx, dy, dist;
  358.         int shift;
  359.         reduce_3(ady3, short_max);
  360.         d = (scaled_sqrt_tab[(ady3 << scaled_sqrt_shift) / adx3] * scaled_flat) >> sqrt_scale_shift;
  361.         dx = x1 - x0, dy = y1 - y0;
  362.         reduce_d(dx);
  363.         if ( ((dist = ((dx * dy3 / dx3) << shift) - dy) < 0 ?
  364.               -dist : dist) > d )
  365.           goto sub;    /* not flat enough */
  366.         dx = x2 - x0, dy = y2 - y0;
  367.         reduce_d(dx);
  368.         if ( ((dist = ((dx * dy3 / dx3) << shift) - dy) < 0 ?
  369.               -dist : dist) > d )
  370.           goto sub;    /* not flat enough */
  371.         }
  372.        else if ( ady3 != 0 )
  373.         {    fixed d, dy, dx, dist;
  374.         int shift;
  375.         reduce_3(adx3, short_max);
  376.         d = (scaled_sqrt_tab[(adx3 << scaled_sqrt_shift) / ady3] * scaled_flat) >> sqrt_scale_shift;
  377.         dy = y1 - y0, dx = x1 - x0;
  378.         reduce_d(dy);
  379.         if ( ((dist = ((dy * dx3 / dy3) << shift) - dx) < 0 ?
  380.               -dist : dist) > d )
  381.           goto sub;    /* not flat enough */
  382.         dy = y2 - y0, dx = x2 - x0;
  383.         reduce_d(dy);
  384.         if ( ((dist = ((dy * dx3 / dy3) << shift) - dx) < 0 ?
  385.               -dist : dist) > d )
  386.           goto sub;    /* not flat enough */
  387.         }
  388.      }
  389.     /* Curve is flat enough.  Add a line and exit. */
  390. #ifdef DEBUG
  391. if ( gs_debug['2'] )
  392.     dprintf2("[2]\t*** x=%f, y=%f ***\n",
  393.          fixed2float(x3), fixed2float(y3));
  394. #endif
  395.     return gx_path_add_line(ppath, x3, y3);
  396.  
  397.     /* Curve isn't flat enough.  Break into two pieces and recur. */
  398.     /* Algorithm is from "The Beta2-split: A special case of the */
  399.     /* Beta-spline Curve and Surface Representation," B. A. Barsky */
  400.     /* and A. D. DeRose, IEEE, 1985, courtesy of Crispin Goswell. */
  401. sub:
  402. #define midpoint(a,b) arith_rshift((a) + (b), 1)
  403.    {    fixed x01 = midpoint(x0, x1), y01 = midpoint(y0, y1);
  404.     fixed x12 = midpoint(x1, x2), y12 = midpoint(y1, y2);
  405.     fixed x02 = midpoint(x01, x12), y02 = midpoint(y01, y12);
  406.     int code;
  407.     /* Update x/y1, x/y2, and x/y0 now for the second half. */
  408.     x2 = midpoint(x2, x3), y2 = midpoint(y2, y3);
  409.     x1 = midpoint(x12, x2), y1 = midpoint(y12, y2);
  410.     code = flatten_recur(ppath, x01, y01, x02, y02,
  411.         (x0 = midpoint(x02, x1)), (y0 = midpoint(y02, y1)),
  412.         scaled_flat);
  413.     if ( code < 0 ) return code;
  414.    }    goto top;
  415. }
  416.